Ext.apply(Sys.fn, {
	keyEnterEsc : function(keyCode, e, items) {
		var id = e.getTarget().id;
		var c = null, index = 0;
		if (!items)
			items = Ext.getCmp(id).ownerCt.items;
		items.each(function(v, i, a) {
					if (v.getXType() != 'fieldset' && Ext.isFunction(v.getId)
							&& (id == v.getId() || id.indexOf(v.getId()) == 0)) {
						index = i;
						c = v;
						return false;
					}
				});
		if (!c)
			return false;
		// 判断获取控件焦点
		if (keyCode == Ext.EventObject.ENTER || keyCode == Ext.EventObject.ESC) {
			if (keyCode == Ext.EventObject.ENTER && (index + 1) < items.length) {
				// 判断输入是否有效
				if (!c.isValid()) {
					return false;
				}
			}
			var nextC = null;
			var isENTER = (keyCode == Ext.EventObject.ENTER) // 是否为回车
			do {
				index += isENTER ? 1 : -1;
				if ((index >= items.length && isENTER)
						|| (index < 0 && !isENTER)) {
					return false;
				}
				nextC = items.get(index);
			} while (nextC.disabled || nextC.hidden || nextC.readOnly
					|| !Ext.isFunction(nextC.focus)
					|| !Ext.isFunction(nextC.selectText)
			)
			nextC.focus(true, 200);
			if (keyCode == Ext.EventObject.ESC && (index - 1 >= 0)) {
				c.reset();
				c.clearInvalid();
			}
		}
		return false;
	},
	compareObject : function(o1, o2) {
		if (typeof o1 != typeof o2)
			return false;
		if (typeof o1 == 'object') {
			for (var o in o1) {
				if (typeof o2[o] == 'undefined')
					return false;
				if (!Sys.fn.compareObject(o1[o], o2[o]))
					return false;
			}
			return true;
		} else {
			return o1 === o2;
		}
	},
	// 将表格数据导出到Excel
	exportToExcel : function(grid, isAll, fielName) {
		var start = 0, limit = 0
		var pg = {};
		var pgb = grid.getBottomToolbar();
		if (pgb) {
			try {
				limit = pgb.pageSize;
				start = pgb.cursor;
			} catch (e) {

			}
		}
		var FieldList = [];
		var cm = grid.colModel;
		var _store = grid.getStore();
		var _para = _store.baseParams;
		var _url = _store.url;
		var v = '';
		var p = '';
		if (_store.getCount() == 0) {
			alert("您还没有查询数据！");
			return
		}
		if (grid.store.proxy.api.create.url) {
			_url = String(grid.store.proxy.api.create.url);
		}
		FieldList = '[';
		for (var i = 0, len = cm.getColumnCount(); i < len; i++) {
			if (!cm.isHidden(i) && cm.config[i].dataIndex) { // 没有隐藏，并且是有绑定数据。避免多选框和数字框
				// FieldList.push({Header:cm.columns[i].header,Field:cm.columns[i].dataIndex});
				if (i > 0 && FieldList != '[') {
					FieldList += ",";
				}
				FieldList += "{Header:'" + cm.config[i].header + "',Field:'"
						+ cm.config[i].dataIndex + "'}";
			}
		}
		FieldList += ']';
		// FieldList = String(Ext.util.JSON.encode(FieldList));
		var _FileName = grid._FileName || grid.title;
		Ext.apply(_para, {
					_OpType : 'excel',
					_FileName : fielName || grid.title,
					_FieldList : FieldList,
					start : isAll ? 0 : start,
					limit : isAll ? 0 : limit
				});
		if (_store.getSortState()) {
			_para.sort = _store.getSortState().field;
			_para.dir = _store.getSortState().direction;
		}

		Sys.fn.fdFormSubmit(_para, _url);
		// 还原store的参数值，防止用户导出后在点查询时_OpType参数还存在
		Ext.apply(_store.baseParams, {
					_OpType : null,
					_FieldList : null,
					start : start,
					limit : limit
				});
	},
	fdFormSubmit : function(_para, _url) {
		fd = document.getElementById("frmDummy");
		if (fd) {
			var childs = fd.childNodes;
			if (childs && childs.length > 0) {
				for (var i = childs.length - 1; i >= 0; i--) {
					fd.removeChild(childs[i]);
				}
			}
			fd.parentNode.removeChild(fd);
		}

		fd = Ext.DomHelper.append(Ext.getBody(), {
					tag : 'form',
					method : 'post',
					id : 'frmDummy',
					action : _url,
					name : 'frmDummy',
					cls : 'x-hidden'
				}, true);
		for (p in _para) {
			if (!_para.getElementsByTagName) {
				v = _para[p];
				Ext.DomHelper.append('frmDummy', {
							tag : 'input',
							id : p,
							name : p,
							cls : 'x-hidden',
							value : v
						});
			}
		}
		fd.dom.submit();
	},
	// 打印车票
	PrintTicket : function(_rs, _packet) {
		if (_rs.length == 0)
			return false;
		var TicketMsg = '', InsMsg1 = '', InsMsg2 = ''
		PrintClear();

		Ext.each(_rs, function(r, i, a) {
					TicketMsg = TicketMsg + r.PrintStr;
					InsMsg1 = InsMsg1 + (InsMsg1 == '' ? '' : '\r\n')
							+ r.InsMsg1;
					InsMsg2 = InsMsg2 + (InsMsg2 == '' ? '' : '\r\n')
							+ r.InsMsg2;
				}, this);
		if (InsMsg1 != '') {
			PrintClear();
			PrintAppend(InsMsg1);
			PrintToFile("C:\\TKINS.TXT");
		}
		if (InsMsg2 != '') {
			PrintClear();
			PrintAppend(InsMsg2);
			PrintToFile("c:\\chyx\\ticket\\ticket_" + Sys.User.LoginID + ".TXT");
		}
		if (!true) { // 指令方式打印
			PrintClear();
			PrintAppend(TicketMsg);
			PrintToFile("c:\\_PrintRecord.txt");
			PrintTo("LPT1");
		} else { // 报表方式打印
			// 由于票样使用了"无限页高"选项,打印10张会走空纸,所以用变量单页打印
			// alert(_packet);
			PrintClear();
			PrintAppend(_packet);
			PrintToFile("c:\\_PrintRecord.txt");
			ReportPrint("Ticket.fr3", _packet);

		}
	},
	getSaleType : function(TypeCode) {
		var result;
		if (!isNaN(TypeCode))
			Ext.each(Sys.data.CoreSaleType, function(v) {
						if (v.TypeFlag == TypeCode) {
							result = v;
							return false
						}
					});
		else
			Ext.each(Sys.data.CoreSaleType, function(v) {
						if (v.TypeCode == TypeCode) {
							result = v;
							return false
						}
					});

		return result;
	},
	orderPrintGetLast : function(ids) {
		Ext.Ajax.request({
					url : Sys.url.Orders.orderPrintGetLast,
					scope : this,
					params : {
						OperID : Sys.User.ID,
						OperCode : Sys.User.LoginID,
						OperName : Sys.User.UserName,
						OpNodeID : Sys.WorkNodeSet.ID,
						TicketStyle : 2, // 票样
						PrintType : ids ? 2 : 1,
						SaleDetailIDs : ids
					},
					success : function(response, opts) {
						// this.doConfirmBillNo(); // 重新获取用户当前票号
						var obj = Ext.decode(response.responseText);
						Sys.fn.PrintTicket.defer(200, this, [obj.data,
										obj.Packet]);
					},
					failure : function(response, opts) {
						Ext.Msg.showRError(response, opts, '获取打印车票');
					}
				});
	}
});